﻿using Microsoft.Extensions.DependencyInjection;
using QPlcPlayer.Models;
using QPlcPlayer.Services;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace QPlcPlayer
{
	public partial class SettingFeedFrm : Form
	{
		public SettingFeedFrm()
		{
			InitializeComponent();
			InitDosingSchedule();
		}


		public void InitDosingSchedule()
		{
			var car = AppRuntime.GetService().GetRequiredService<AgvCarService>().GetAgvCarByNumber(AppRuntime.CarNumber);
			if (car == null) return;
			var info = AppRuntime.GetService().GetRequiredService<DosingScheduleService>().GetDosingScheduleByCarId(car.ID);
			if (info != null)
			{
				carNumber.Text = car.CarNumber;
				firstHour.Value = info.HourFirst;
				firstMinute.Value = info.MinuteFirst;
				first_80.Value = info.First_80;
				first_120.Value = info.First_120;
				first_160.Value = info.First_160;
				first_200.Value = info.First_200;

				secondHour.Value = info.HourSecond;
				secondMinute.Value = info.MinuteSecond;
				second_80.Value = info.Second_80;
				second_120.Value = info.Second_120;
				second_160.Value = info.Second_160;
				second_200.Value = info.Second_200;


				thirdHour.Value = info.HourThird;
				thirdMinute.Value = info.MinuteThird;
				third_80.Value = info.Third_80;
				third_120.Value = info.Third_120;
				third_160.Value = info.Third_160;
				third_200.Value = info.Third_200;


			}
		}

		/// <summary>
		/// 保存数据
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button1_Click(object sender, EventArgs e)
		{
			var car = AppRuntime.GetService().GetRequiredService<AgvCarService>().GetAgvCarByNumber(AppRuntime.CarNumber);
			var info = AppRuntime.GetService().GetRequiredService<DosingScheduleService>().GetDosingScheduleByCarId(car.ID);

			var data = new DosingSchedule();
			data.CarId = car.ID;
			if (info != null) data.ID = info.ID;
			data.HourFirst = (int)firstHour.Value;
			data.MinuteFirst = (int)firstMinute.Value;

			data.HourSecond = (int)secondHour.Value;
			data.MinuteSecond = (int)secondMinute.Value;

			data.HourThird = (int)thirdHour.Value;
			data.MinuteThird = (int)thirdMinute.Value;
			var peifangs = new List<int[]>
			{
				new int[]{(int)first_80.Value,(int)second_80.Value,(int)third_80.Value},
				new int[]{(int)first_120.Value,(int)second_120.Value,(int)third_120.Value},
				new int[]{(int)first_160.Value,(int)second_160.Value,(int)third_160.Value},
				new int[]{(int)first_200.Value,(int)second_200.Value,(int)third_200.Value}
			};
			var isError = false;
			var msg = "";
			for(int i = 0; i < peifangs.Count; i++)
			{
				if (peifangs[i].Sum() != (i + 2) * 40)
				{
					isError=true;
					msg=$"{(i + 2) * 40}喂料配方总和应该为:{(i + 2) * 40}";
					break;
					
				}
				for(int j = 0; j < peifangs[i].Length;j++)
				{
					if (peifangs[i][j] % 40 != 0)
					{
						isError=true;
						msg = $"{(i + 2) * 40}喂料配方中第{j+1}次喂料量必须为40的整数倍";
						break;
					}
				}
				if (isError) break;
			}
			if (isError)
			{
				MessageBox.Show(msg);
				return;
			}
			data.First_80 = peifangs[0][0];
			data.Second_80 = peifangs[0][1];
			data.Third_80 = peifangs[0][2];
			data.First_120 = peifangs[1][0];
			data.Second_120=peifangs[1][1];
			data.Third_120=peifangs[1][2];
			data.First_160=peifangs[2][0];
			data.Second_160 = peifangs[2][1];
			data.Third_160 = peifangs[2][2];
			data.First_200 = peifangs[3][0];
			data.Second_200= peifangs[3][1];
			data.Third_200= peifangs[3][2];

			AppRuntime.GetService().GetService<DosingScheduleService>().AddOrUpdateDosingSchedule(data);

			MessageBox.Show("保存成功");
			Task.Run(() =>
			{
				AppRuntime.UpdateData(); 

			});
		}
	}
}
